from google.appengine.ext import db
import datetime
import urllib
from BeautifulSoup import BeautifulSoup
import logging
import string
import xmw
class PAGE_TYPE:
    LINK = "LINK"
    FORM = "FORM"
        
class Tianya_Thread(xmw.Thread):
    page_type=db.StringProperty(multiline=False)  #LINK or FORM    
    last_scan_post=db.DateTimeProperty()    
    last_scan_thread=db.DateTimeProperty()            
    #status=db.StringProperty(required=True,default  = xmw.Thread_Status.INIT)
    offset =db.IntegerProperty(default =0)

    def refresh(self):
        page=self.get_first_page()
        is_bad=page.is_BAD()
        if is_bad:
            self.status = xmw.Thread_Status.BAD
            return None
        self.title=page.get_title()
        self.last_scan=datetime.datetime.today()
        self.page_type=page.get_page_type()
        logging.info("page type:%s"%self.page_type)
        self.page_cnt=page.get_form_cnt()
        self.chrAuthor=page.get_chrAuthor()
        #self.status=xmw.Thread_Status.RUN
        
        self.last_scan_thread = datetime.datetime.now()
        self.put()
        return self

    def load_page(self,offset):
        #assert self.page_type ="FORM"
        #page_number < self.page_cnt
        #page_number >=0
        url = self.lst_page_url[0]
        first_page = self.get_first_page()
        if first_page ==0:
            return first_page
        
        #read post params      
        rs_strTitle_aa = first_page.get_rs_strTitle_aa()
        intLogo = first_page.get_intLogo()
        rs_permission = first_page.get_rs_permission()
        apn = first_page.get_apn()
        
        str_page_number = "" + str(offset+1)
        
        #I found that only apn & pID are needed
        params_dict={
                               'rs_strTitle_aa':repr(rs_strTitle_aa),\
                               'intLogo':intLogo,\
                               'rs_permission':rs_permission,\
                               'apn':apn,\
                               'pID':str_page_number
                               }
        logging.info("send post req:%s"%params_dict)
        params = urllib.urlencode(params_dict)
        
        sock = urllib.urlopen(url, params)
        html = sock.read()
        #self.soup = BeautifulSoup(soc, fromEncoding="GB18030")
        sock.close()
        
        page    =TianyaPage(html,url,self.thread_id,offset)
        return page
        
    def get_last_page(self):
        page = self.walk_to_page(self.page_cnt-1)
        return page
    
    def get_first_page(self):
        url = self.lst_page_url[0]
        sock = urllib.urlopen(url)
        html = sock.read()            
        sock.close()
        page    =TianyaPage(html,url,thread_id=self.thread_id,offset=0)

        return page

class TianyaPage:
    def __init__(self,html,url,thread_id,offset):
        self.html=html
        self.url=url
        self.soup=BeautifulSoup(html, fromEncoding="GB18030")
        self.thread_id=thread_id
        self.offset=offset
    
    def get_lst_post(self):
        lst_table   =self.soup.findAll('table',{'align':'center','width':'100%'})
        lst_content =self.soup.findAll('div',{'class':'content','style':'WORD-WRAP:break-word'})
        logging.info("read %d tables"%len(lst_table))
        logging.info("read %d contents"%len(lst_content))
        post_cnt=len(lst_content)
        
        lst_post=[]
        index=0
        """
        print "table:%s"%lst_table[len(lst_table)-1]
        print "content:%s"%lst_content[len(lst_content)-1]
        return lst_post
        """
        length=0
        if self.offset ==0:
            length=post_cnt
        else:
            length =post_cnt -1
            
        while index < length:
            if self.offset==0:
                content=lst_content[index]
            else:
                content=lst_content[index+1]
                
            table=lst_table[index+1]
            """
            #in first page,table counts 1 more
            if self.offset ==0:
                table=lst_table[index+1]
            else:
                table=lst_table[index]
            """
            #print "<B>TABLE %d</B>:%s"%(index,table)
            #print "<B>CONTENT %d</B>:%s"%(index,content)
            
            
            ht=Head_Table(table,self)
            

            #logging.info("CONTENT:%s"%repr(content))
            cc=""
            for c in content.contents:
                cc =cc+ str(c).decode("utf-8")
            #content1=str(content.contents[0]).decode("utf-8")
            content1    =cc
            #content2=content1.encode('ascii')
            #logging.info("url=%s"%self.url)
            #print len(content1)
            #ht.get_author(),\
            logging.info("author:%s,floor:%s"%(ht.get_author(),ht.get_floor()))
            uid=xmw.get_uuid_hex()
            post=Post(author=ht.get_author(),\
                      date=ht.get_post_date(),\
                      floor=ht.get_floor(),\
                      #floor="floor",\
                      content=content1,\
                      #content="content",\
                      url=(self.url).encode('ascii'),\
                      post_id=uid,\
                      thread_id = self.thread_id,\
                      offset = self.offset)
            #post.put()
            lst_post.append(post)
            index=index+1
        
        return lst_post

         
    def is_BAD(self):
        div=self.soup.find('div',{'class':'x'})
        #print type(div)
        if div is None:
            return False
        else:
            return True
        
        

    
    def get_chrAuthor(self):
        author_input=self.soup.find('input', {'name':'chrAuthor'})
        #print author_input
        chrAuthor=author_input['value']
        #print chrAuthor
        #print "author is %s"%chrAuthor
        #print "len =%s"%str(len(chrAuthor))
        is_from_soup=True
        if len(chrAuthor) ==0:
            is_from_soup=False
            #print "len ==0"
            ss=self.html.split("name=\"chrAuthor\" value=")[1]
            
            ss=ss.split(">")
            #print "ss=%s"%str(ss)
            chrAuthor=(ss[0]).decode("GB18030")
        
        if is_from_soup is True:
            chrAuthor=chrAuthor.decode('utf-8')
            
        return chrAuthor
        
    def get_title(self):
        title_tag = self.soup.find('title')
        title=str(title_tag.contents[0])
        #print str(title_tag.contents[0])
        
        #print "title=%s"%title.decode('utf-8')
        return title.decode('utf-8')
        #return self.get_rs_strTitle_aa()
        
    def get_page_type(self):
        lst_pageForm = self.soup.find('form', id='pageForm')
    
        if (lst_pageForm is not None):
            return PAGE_TYPE.FORM 
        else:
            return PAGE_TYPE.LINK
    
    def get_form_cnt(self):
    
        apn = self.soup.find('input', {'name':'apn'})
        lst_pages = string.split(apn['value'], ',')
        #print len(lst_pages)
        return len(lst_pages)

    
    def get_rs_strTitle_aa(self):
        
        rs_strTitle_aa = self.soup.find('input', {'name':'rs_strTitle_aa'})

        return rs_strTitle_aa['value']
    def get_intLogo(self):
        intLogo = self.soup.find('input', {'name':'intLogo'})
        return intLogo['value']

    def get_rs_permission(self):
        rs_permission = self.soup.find ('input', {'name':'rs_permission'})
        return rs_permission['value']
    def get_apn(self):
        apn = self.soup.find('input', {'name':'apn'})
        return apn['value']    